<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {
	public $helpers = array('Acl');
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
			$this->Session->setFlash(__('User deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('User was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

	public function login() {
	    if ($this->request->is('post')) {
	        if ($this->Auth->login()) {
	            $this->redirect($this->Auth->redirect());
	        } else {
	            $this->Session->setFlash('Your username or password was incorrect.');
	        }
	    }
	}

	public function permissions($id){
		$this->loadModel('Acos');
		if (!empty($this->request->query)) {
            $req = $this->request->query;
            $aco = $this->Acos->find('first', array('conditions' => array('alias' => $req['aco'])));
            //die(var_dump($aco));
            if ($req['perm'] == 'allow') {
            	
                $this->Acl->allow(array('User' => array('id' => $req['aro'])), $req['aco']);
                if(count($aco) && count($aco['Filhos']))
                	foreach ($aco['Filhos'] as $son) {
                		$this->Acl->allow(array('User' => array('id' => $req['aro'])), $req['aco'].'/'.$son['alias']);
                	}
                $this->Session->setFlash('Aro está permitido');
            } elseif ($req['perm'] == 'deny') {
                $this->Acl->deny(array('User' => array('id' => $req['aro'])), $req['aco']);
                if(count($aco) && count($aco['Filhos']))
                	foreach ($aco['Filhos'] as $son) {
                		$this->Acl->deny(array('User' => array('id' => $req['aro'])), $req['aco'].'/'.$son['alias']);
                	}
                $this->Session->setFlash('Aro está negado');
            }
        }
		$acos = $this->Acos->find('all', array('order' => array('id' => 'DESC')));
		$this->set('acos', $acos);
		$this->set('id', $id);
		$perm = array();
		$perm[30] = true;
		foreach ($acos as $aco) {
			
			if(count($aco['Filhos']))
			{
				$perm[$aco['Acos']['id']] = $this->Acl->check(array('User' => array('id' => $id)), $aco['Acos']['alias']);
				foreach ($aco['Filhos'] as $son) {
					if($aco['Acos']['id'] != 1)
						$perm[$son['id']] = $this->Acl->check(array('User' => array('id' => $id)), $aco['Acos']['alias'].'/'.$son['alias']);

					if($perm[$son['id']])
						$perm[$aco['Acos']['id']] = true;
					
				}
			}
				
		}
		$this->Acl->deny(array('Group' => array('id' => 2)), 'Widgets');
		$this->set('perm', $perm);
	}

	public function logout() {
	    $this->Session->setFlash('Good-Bye');
		$this->redirect($this->Auth->logout());
	}

	public function beforeFilter() {
	    parent::beforeFilter();
	}
}
